/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.html;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * <p>Adds query parameter with random value to the specified URI.
 * This can be useful for links that point to dynamically generated images
 * (for example, servlet that selects an image by its ID and does some
 * transformations on it before output), because browsers usually cache image
 * with specific URL even if headers forbid this. In this case, you may use
 * this tag to make URI 'unique', so image will not be cached.
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>value</b> - required - URI that needs to be made 'unique'
 * </li>
 * <li>
 * <b>var</b> - name of variable that will accept result. If not specified,
 * result will be written to page.
 * </li>
 * <li>
 * <b>scope</b> - scope of variable that will accept result. If <b>var</b> is
 * not specified, this is ignored.
 * </li>
 * </ul>
 * </p>
 * <p>
 * Here's an example:
 * <pre>
 * &lt;img src="&lt;atleap:randomLink value="${ctxPath}/servlet/thumbnail/" /&gt;" border="0" /&gt;
 * </pre>
 * IMG tag will have unique src each time page is displayed.
 * </p>
 * <p><a href="RandomLinkTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.9 $ $Date: 2006/08/03 10:08:23 $
 * @jsp.tag name="randomLink"
 * body-content="empty"
 */
public class RandomLinkTag extends SimpleTagSupport {

	protected transient final Log log = LogFactory.getLog(RandomLinkTag.class);

	/**
	 * Value to rewrite
	 */
	protected String value;

	/**
	 * Name of variable to export calculated value
	 */
	protected String var;

	/**
	 * Scope to export variable to
	 */
	protected String scope;

	/**
     * Returns value to rewrite
     *
	 * @return value to rewrite
	 * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Value to rewrite"
	 */
	public String getValue() {
		return value;
	}

    /**
     * Sets value to rewrite
     *
     * @param value value to rewrite to set
     */
	public void setValue(String value) {
		this.value = value;
	}

	/**
     * Returns name of variable which will accept the result
     *
     * @return name of variable
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of variable to export message"
	 */
	public String getVar() {
		return var;
	}

    /**
     * Sets name of variable which will accept the result
     *
     * @param var name of variable to set
     */
	public void setVar(String var) {
		this.var = var;
	}

	/**
     * Returns variable scope
     *
     * @return variable scope
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Scope to export variable to"
	 */
	public String getScope() {
		return scope;
	}

    /**
     * Sets variable scope
     *
     * @param scope variable scope to set
     */
	public void setScope(String scope) {
		this.scope = scope;
	}

    /**
     * Processes the tag
     *
     * @throws JspException
     */
	public void doTag() throws JspException {

		PageContext pageContext = (PageContext) getJspContext();

		double randomValue = Math.random();
		String rnd = "rnd=" + randomValue;
		if ( value.indexOf('?') != -1 ) {
			value += "&" + rnd;
		} else {
			value += "?" + rnd;
		}

		TagUtils tagUtils = TagUtils.getInstance();
		if ( var != null ) {
			// save value in specified scope
			int varScope = PageContext.PAGE_SCOPE;
			if ( scope != null ) {
				varScope = tagUtils.getScope(scope);
			}
			pageContext.setAttribute(var, value, varScope);

		} else {
			tagUtils.write(pageContext, value);
		}
	}
}
